package com.ruoyi.project.game.game.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.game.assotag.domain.AssoTag;
import com.ruoyi.project.game.assotag.mapper.AssoTagMapper;
import com.ruoyi.project.game.game.domain.Game;
import com.ruoyi.project.game.game.mapper.GameMapper;
import com.ruoyi.project.game.game.service.IGameService;
import com.ruoyi.project.game.gamecategory.domain.GameCategory;
import com.ruoyi.project.game.gamecategory.mapper.GameCategoryMapper;
import com.ruoyi.project.game.gametag.domain.GameTag;
import com.ruoyi.project.game.gametag.mapper.GameTagMapper;
import com.ruoyi.project.game.recommend.service.IRecommendService;

/**
 * H5游戏Service业务层处理
 * 
 * @author shixiangwen
 * @date 2020-02-10
 */
@Service
@DataSource(value = DataSourceType.GAME)
public class GameServiceImpl implements IGameService {

	@Autowired
	GameMapper gameMapper;

	@Autowired
	GameTagMapper gameTagMapper;

	@Autowired
	AssoTagMapper assoTagMapper;

	@Autowired
	GameCategoryMapper gameCategoryMapper;

	@Autowired
	IRecommendService recommendService;

	/**
	 * 查询H5游戏
	 * 
	 * @param id H5游戏ID
	 * @return H5游戏
	 */
	@Override
	public Game selectGameById(Long id) {
		return gameMapper.selectGameById(id);
	}

	/**
	 * 查询H5游戏列表
	 * 
	 * @param game H5游戏
	 * @return H5游戏
	 */
	@Override
	public List<Game> selectGameList(Game game) {
		return gameMapper.selectGameList(game);
	}

	/**
	 * 新增H5游戏
	 * 
	 * @param game H5游戏
	 * @return 结果
	 */
	@Override
	public int insertGame(Game game) {
		return gameMapper.insertGame(game);
	}

	/**
	 * 修改H5游戏
	 * 
	 * @param game H5游戏
	 * @return 结果
	 */
	@Override
	@Transactional
	public int updateGame(Game game) {

		insertGameTag(game);

		insertGameCategory(game);

		insertAssoTag(game);

		Game oldGame = this.selectGameById(game.getId());
		if (oldGame.getStatus() != 3 && game.getStatus() == 3) {
			game.setDeployTime(DateUtils.getNowDate());
		}
		if (game.getRecom() == 1 && oldGame.getRecom() == 0) {
			recommendService.addRecommend(game.getId());
		} else if (game.getRecom() == 0 && oldGame.getRecom() == 1) {
			recommendService.delRecommend(game.getId());
		}

		return gameMapper.updateGame(game);
	}

	private void insertAssoTag(Game game) {

		Long gameId = game.getId();

		assoTagMapper.deleteAssoTagByGameId(gameId);

		Date now = DateUtils.getNowDate();
		String loginName = ShiroUtils.getLoginName();

		List<AssoTag> assoTags = new ArrayList<>();
		for (Long tagId : game.getGameTagIds()) {
			AssoTag assoTag = new AssoTag();
			assoTag.setCreateBy(loginName);
			assoTag.setCreateTime(now);
			assoTag.setUpdateBy(loginName);
			assoTag.setUpdateTime(now);
			assoTag.setGameId(gameId);
			assoTag.setTagId(tagId);
			assoTags.add(assoTag);
		}

		if (assoTags.size() > 0) {
			assoTagMapper.batchAssoTag(assoTags);
		}
	}

	private void insertGameCategory(Game game) {

		Long gameId = game.getId();

		gameCategoryMapper.deleteGameCategoryByGameId(gameId);

		Date now = DateUtils.getNowDate();
		String loginName = ShiroUtils.getLoginName();

		List<GameCategory> gameCategorys = new ArrayList<>();
		for (Long categoryId : game.getGameCategoryIds()) {
			GameCategory gameCategory = new GameCategory();
			gameCategory.setCreateBy(loginName);
			gameCategory.setCreateTime(now);
			gameCategory.setUpdateBy(loginName);
			gameCategory.setUpdateTime(now);
			gameCategory.setGameId(gameId);
			gameCategory.setCategoryId(categoryId);
			gameCategorys.add(gameCategory);
		}

		if (gameCategorys.size() > 0) {
			gameCategoryMapper.batchGameCategory(gameCategorys);
		}
	}

	private void insertGameTag(Game game) {

		Long gameId = game.getId();

		gameTagMapper.deleteGameTagByGameId(gameId);

		Date now = DateUtils.getNowDate();
		String loginName = ShiroUtils.getLoginName();

		List<GameTag> gameTags = new ArrayList<>();
		for (Long tagId : game.getGameTagIds()) {
			GameTag gameTag = new GameTag();
			gameTag.setCreateBy(loginName);
			gameTag.setCreateTime(now);
			gameTag.setUpdateBy(loginName);
			gameTag.setUpdateTime(now);
			gameTag.setGameId(gameId);
			gameTag.setTagId(tagId);
			gameTags.add(gameTag);
		}

		if (gameTags.size() > 0) {
			gameTagMapper.batchGameTag(gameTags);
		}
	}

	/**
	 * 删除H5游戏对象
	 * 
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteGameByIds(String ids) {
		return gameMapper.deleteGameByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除H5游戏信息
	 * 
	 * @param id H5游戏ID
	 * @return 结果
	 */
	@Override
	public int deleteGameById(Long id) {
		return gameMapper.deleteGameById(id);
	}
}
